import { Injectable } from '@angular/core';
import { defaultKdsConfig } from './../../config';
import { OrderConfig, KdsConfig, SoundConfig, AppConfig, ExpeditionOrderConfig } from './../models/kdsConfig';
import { ServiceBaseX } from './order';

@Injectable()
export class ConfigSvr extends ServiceBaseX {
    constructor() {
        super();
        if (!this.kdsConfig) {
            this.init();
        }
    }
    init() {
        this.kdsConfig = defaultKdsConfig;
    }

    private _appConfig: AppConfig;
    get appConfig(): AppConfig {
        if (!this._appConfig) {
            this._appConfig = this.kdsConfig.appConfig;
        }
        return this._appConfig;
    }
    set appConfig(appConfig: AppConfig) {
        this._appConfig = appConfig;

        const kdsConfig = this.kdsConfig;
        kdsConfig.appConfig = appConfig;
        this.kdsConfig = kdsConfig;
    }



    private _orderConfig: OrderConfig;
    get orderConfig(): OrderConfig {
        if (!this._orderConfig) {
            this._orderConfig = this.kdsConfig.orderConfig;
        }
        return this._orderConfig;
    }
    set orderConfig(orderConfig: OrderConfig) {
        this._orderConfig = orderConfig;

        const kdsConfig = this.kdsConfig;
        kdsConfig.orderConfig = orderConfig;
        this.kdsConfig = kdsConfig;
    }

    private _soundConfig: SoundConfig;
    get soundConfig(): SoundConfig {
        if (!this._soundConfig) {
            this._soundConfig = this.kdsConfig.soundConfig;
        }
        return this._soundConfig;
    }
    set soundConfig(soundConfig: SoundConfig) {
        this._soundConfig = soundConfig;

        const kdsConfig = this.kdsConfig;
        kdsConfig.soundConfig = soundConfig;
        this.kdsConfig = kdsConfig;
    }

    //这里可以再抽象
    private _expeditionOrderConfig: ExpeditionOrderConfig;
    get expeditionOrderConfig(): ExpeditionOrderConfig {
        if (!this._expeditionOrderConfig) {
            this._expeditionOrderConfig = this.kdsConfig.expeditionOrderConfig;
        }
        return this._expeditionOrderConfig;
    }
    set expeditionOrderConfig(soundConfig: ExpeditionOrderConfig) {
        this._expeditionOrderConfig = soundConfig;

        const kdsConfig = this.kdsConfig;
        kdsConfig.expeditionOrderConfig = soundConfig;
        this.kdsConfig = kdsConfig;
    }

    get kdsConfig(): KdsConfig {
        let kdsConfig = localStorage.getItem("kdsConfig");
        if (kdsConfig) {
            return JSON.parse(kdsConfig);
        }
        return null;
    }
    set kdsConfig(kdsConfig: KdsConfig) {
        localStorage.setItem("kdsConfig", JSON.stringify(kdsConfig));
    }

    get lastConfig(): any {
        let lastConfig = localStorage.getItem("lastConfig");
        if (lastConfig) {
            return JSON.parse(lastConfig);
        }
        return null;
    }
    set lastConfig(lastConfig: any) {
        localStorage.setItem("lastConfig", JSON.stringify(lastConfig));
    }

    resetTerminal() {
        //清空localStorage 重新跳转
        const localStorage = <any>window.localStorage;
        // for (const toreage of <any>window.localStorage) {
        // }
        localStorage.clear();
        setTimeout(() => {
            location.href = location.origin + "/local/expediter/index.html";
            location.reload();
        }, 200);
    }

    get currentTerminalInfo() {
        const result = [];
        result.push(new CustomObject("terminalName", this.currentDevice.name, "本终端名称"));
        result.push(new CustomObject("terminalIp", this.adsAbout.url.split("//")[1].split(":")[0], "本终端IP地址"));
        result.push(new CustomObject("ldcIp", this.LDCConfig.ip, "本地数据中心IP地址"));
        return result;
    }
}


export class CustomObject {
    key: string;
    value: any;
    title: string;
    subhead?: string;
    description?: string;
    constructor(key, value, title) {
        this.key = key;
        this.value = value;
        this.title = title;
    }
}


